import java.util.HashMap;
import java.util.Map;

public class Solution290 {
    public boolean wordPattern(String pattern, String s) {
        String[] strings=s.split(" ");
        Map<Character,String> map1=new HashMap<>();
        Map<String,Character> map2=new HashMap<>();
        if (strings.length!=pattern.length()){
            return false;
        }
        for (int i=0;i<strings.length;i++){
            if (map1.containsKey(pattern.charAt(i)) || map2.containsKey(strings[i])){
                if (!map1.getOrDefault(pattern.charAt(i),"").equals(strings[i]) || map2.getOrDefault(strings[i],(char)0)!=pattern.charAt(i)){
                    return false;
                }
            }else{
                map1.put(pattern.charAt(i),strings[i]);
                map2.put(strings[i],pattern.charAt(i));
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(new Solution290().wordPattern("abba","dog cat cat dog"));
    }
}
